package com.systop.asset.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.systop.asset.entity.AssetCategories;
import com.systop.asset.entity.vo.AssetCategoryVo;
import com.systop.asset.utils.TreeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.systop.asset.mapper.AssetCategoriesMapper;

import com.systop.asset.service.IAssetCategoriesService;

/**
 * 资产分类Service业务层处理
 * 
 * @author mc
 * @date 2025-06-24
 */
@Service
public class AssetCategoriesServiceImpl implements IAssetCategoriesService 
{
    @Autowired
    private AssetCategoriesMapper assetCategoriesMapper;

    /**
     * 查询资产分类
     * 
     * @param id 资产分类主键
     * @return 资产分类
     */
    @Override
    public AssetCategories selectAssetCategoriesById(Long id)
    {
        return assetCategoriesMapper.selectAssetCategoriesById(id);
    }

    /**
     * 查询资产分类列表
     * 
     * @param assetCategories 资产分类
     * @return 资产分类
     */
    @Override
    public List<AssetCategories> selectAssetCategoriesList(AssetCategories assetCategories)
    {
        return assetCategoriesMapper.selectAssetCategoriesList(assetCategories);
    }

    /**
     * 新增资产分类
     * 
     * @param assetCategories 资产分类
     * @return 结果
     */
    @Override
    public int insertAssetCategories(AssetCategories assetCategories)
    {
        //名称不能重复
        AssetCategories assetCategories1 = assetCategoriesMapper.selectAssetCategoriesByName(assetCategories.getName());
        if (assetCategories1 != null) {
            return 0;
        }
        return assetCategoriesMapper.insertAssetCategories(assetCategories);
    }

    /**
     * 修改资产分类
     * 
     * @param assetCategories 资产分类
     * @return 结果
     */
    @Override
    public int updateAssetCategories(AssetCategories assetCategories)
    {
        //名称不能重复
        AssetCategories assetCategories1 = assetCategoriesMapper.selectAssetCategoriesByName(assetCategories.getName());
        if (assetCategories1 != null && assetCategories1.getId() != assetCategories.getId()) {
            return 0;
        }
        return assetCategoriesMapper.updateAssetCategories(assetCategories);
    }

    /**
     * 批量删除资产分类
     * 
     * @param ids 需要删除的资产分类主键
     * @return 结果
     */
    @Override
    public int deleteAssetCategoriesByIds(Long[] ids)
    {
        return assetCategoriesMapper.deleteAssetCategoriesByIds(ids);
    }

    /**
     * 删除资产分类信息
     * 
     * @param id 资产分类主键
     * @return 结果
     */
    @Override
    public int deleteAssetCategoriesById(Long id)
    {
        return assetCategoriesMapper.deleteAssetCategoriesById(id);
    }

    /**
     * 返回资产分类树形结构
     * @param assetCategories
     * @return
     */
    public List<AssetCategories> getCategories(AssetCategories assetCategories) {
        List<AssetCategories> all = assetCategoriesMapper.selectAssetCategoriesList(assetCategories);
        return TreeUtils.buildTree(all);
    }
    /**
     * 获取所有资产分类数据
     * @param assetCategories
     * @return
     */
    public List<AssetCategoryVo> allList(AssetCategories assetCategories) {
        // 1. 获取所有资产分类数据
        List<AssetCategories> all = assetCategoriesMapper.selectAssetCategoriesList(assetCategories);

        // 2. 创建一个 Map 来存储每个节点的引用
        Map<Long, String> idToNameMap = new HashMap<>();
        for (AssetCategories category : all) {
            idToNameMap.put(category.getId(), category.getName());
        }

        // 3. 构造返回结果
        List<AssetCategoryVo> result = new ArrayList<>();
        for (AssetCategories category : all) {
            AssetCategoryVo vo = new AssetCategoryVo();
            vo.setName(category.getName());
            vo.setId(category.getId());
            Long parentId = category.getParentId();
            if (parentId == 0) {
                vo.setParentName("-");
            } else {
                // 从map中直接获取父类名
                vo.setParentName(idToNameMap.getOrDefault(parentId, "未知"));
            }

            result.add(vo);
        }

        return result;
    }

}
